package com.htu.courseService.picture.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.htu.courseService.common.vo.R;
import com.htu.courseService.picture.service.FamousTeacherService;
import com.htu.courseService.picture.vo.FamousTeacherDto;
import com.htu.courseService.user.vo.UserDTO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/famous")
@RequiredArgsConstructor
@Slf4j
public class FamousTeacherController {

    private final FamousTeacherService famousTeacherService;

    @GetMapping("/list")
    public R<IPage<FamousTeacherDto>> getList(int pageNum,int pageSize){
        IPage<FamousTeacherDto> teacherList = famousTeacherService.getFamousTeacherList(pageNum,pageSize);
        return R.success(teacherList);
    }

    @PostMapping("/add")
    public R<FamousTeacherDto> upload(@RequestParam("file") MultipartFile file){
        FamousTeacherDto famousTeacherDto = famousTeacherService.addPhoto(file);
        return R.success(famousTeacherDto);
    }

    @PostMapping("/insert")
    public R<?> insert(@RequestBody FamousTeacherDto famousTeacherDto){
        famousTeacherService.addFamousTeacherInfo(famousTeacherDto);
        return R.success();
    }

    @DeleteMapping("/delete")
    public R<?> delete(Integer id){
        famousTeacherService.deleteFamousTeacher(id);
        return R.success();
    }

    /**
     * 教师下拉列表
     * @return
     */
    @GetMapping("/getTeacherInfo")
    public R<List<UserDTO>> getTeacherInfo(String username){
        List<UserDTO> userDTOList = famousTeacherService.getTeacherInfo(username);
        return R.success(userDTOList);
    }

    /**
     * 图片预览
     * @param response
     * @param fileUrl
     */
    @GetMapping("/preview/{fileUrl}")
    public void preview(HttpServletResponse response, @PathVariable String fileUrl){
        famousTeacherService.previewFile(response,fileUrl);
    }
}
